package com.baturu.vin.benz.dal.dao;

import com.baturu.vin.benz.dto.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @author liangguanglong
 * @Date 16/7/13
 * @Time 上午11:45
 */
public interface BenzEPCPartsInfoDAO {


    @Results(value = {
            @Result(column = "subgrp", property = "subGroup"),
            @Result(column = "nounidx", property = "partsNameIdx"),
            @Result(column = "codeB", property = "saCode"),
    })
    @Select("select id,catnum,groupnum,subgrp,parttype,partnum,nounidx,descidx,codeb,footnotes from bm_parts2_v where partnum=#{partscode} and parttype=#{partType}")
    List<BenzEPCPartsInfoDTO> findPartsByPartsCode(@Param("partscode") String partsCode, @Param("partType") String partType);

    @Select({"<script>select distinct nounIdx, noun from part_nouns_v",
            " where nounIdx in ",
            "<foreach item='item' index='index' collection='nounIdx' open='(' separator=',' close=') '>",
            "#{item}",
            "</foreach>",
            " and lang=#{lang}",
            "</script>"})
    List<BenzEPCPartsNameDTO> findNounByIdx(@Param("nounIdx") Collection<String> nounIdx, @Param("lang") String lang);

    @Select({"<script>select distinct descIdx, description from part_descs_v",
            " where descIdx in ",
            "<foreach item='item' index='index' collection='descIdx' open='(' separator=',' close=') '>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<BenzEPCPartsNameDTO> findDescriptionByIdx(@Param("descIdx") Collection<String> descIdx);

    @Results(value = {
            @Result(column = "subgrp", property = "subGroup"),
            @Result(column = "nounidx", property = "partsNameIdx"),
            @Result(column = "codeB", property = "saCode"),
            @Result(column = "parttype", property = "partsType"),
            @Result(column = "POS_ADDR", property = "posAddr"),
            @Result(column = "replflg", property = "replFlag")
    })
    @Select({"<script>select id,catnum,groupnum,subgrp,seqnum,seqno,callout,replflg,parttype,partnum,nounidx,",
            "descidx,indent,tuvsign,optflag,optPart,steering,type,reptype,reppno,footnotes,neutral,codeb,",
            "quantbm,components,submods,reppart,POS_ADDR,cbPartsCode from bm_parts2_v   ",
            "  where cbPartsCode in",
            "<foreach item='cbPartsCode' index='index' collection='cbPartsCodes' open='(' separator=',' close=') '>",
            "#{cbPartsCode}",
            "</foreach>",
            " and catnum in ",
            "<foreach item='catNum' index='index' collection='catNums' open='(' separator=',' close=') '>",
            "#{catNum}",
            "</foreach>",
            "</script>"})
    List<BenzEPCPartsInfoDTO> findPartsByPartsCodeAndCatNum(@Param("cbPartsCodes") Set<String> cbPartsCodes, @Param("catNums") List<String> catNums);

    @Results(value = {
            @Result(column = "subgrp", property = "subGroup"),
            @Result(column = "nounidx", property = "partsNameIdx"),
            @Result(column = "codeB", property = "saCode"),
            @Result(column = "parttype", property = "partsType"),
            @Result(column = "POS_ADDR", property = "posAddr"),
            @Result(column = "replflg", property = "replFlag")
    })
    @Select({"<script>select id,catnum,groupnum,subgrp,seqnum,seqno,callout,replflg,parttype,partnum,nounidx,",
            "descidx,indent,tuvsign,optflag,optPart,steering,type,reptype,reppno,footnotes,neutral,codeb,",
            "quantbm,components,submods,reppart,POS_ADDR,cbPartsCode from bm_parts2_v   ",
            "  where cbPartsCode in",
            "<foreach item='cbPartsCode' index='index' collection='cbPartsCodes' open='(' separator=',' close=') '>",
            "#{cbPartsCode}",
            "</foreach>",
            "</script>"})
    List<BenzEPCPartsInfoDTO> findPartsInfoByCode(@Param("cbPartsCodes") Set<String> partsCodes);

    @Results(value = {
            @Result(column = "description", property = "descName")
    })
    @Select({"<script>select id,descidx,description from part_descs_v where lang='Z' and descidx in ",
            "<foreach item='item' index='index' collection='indexes' open='(' separator=',' close=') '>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<BenzEPCPartsDescDTO> findPartsDescByIdx(@Param("indexes") List<String> indexes);

    @Results(value = {
            @Result(column = "description", property = "descName"),
            @Result(column = "descidx", property = "descIdx")
    })
    @Select({"<script>select id,descidx,description from part_descs_v where  description in ",
            "<foreach item='item' index='index' collection='descs' open='(' separator=',' close=') '>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<BenzEPCPartsDescDTO> findPartsDescByDesc(@Param("descs") List<String> partsDescList);

    @Results(value = {
            @Result(column = "subgrp", property = "subGroup"),
            @Result(column = "nounidx", property = "partsNameIdx"),
            @Result(column = "codeB", property = "saCode"),
            @Result(column = "parttype", property = "partsType"),
            @Result(column = "replflg", property = "replFlag"),
            @Result(column = "SUBMODS", property = "engineEnd3Str")
    })
    @Select({"<script>select id,catnum,groupnum,subgrp,parttype,partnum,nounidx,descidx,codeb,footnotes,replflg,SUBMODS,steering from bm_parts2_v where  " +
            "  type in ",
            "<foreach item='type' index='index' collection='types' open='(' separator=',' close=') '>",
            "#{type}",
            "</foreach>",
            " and nounidx in ",
            "<foreach item='item' index='index' collection='nounidxList' open='(' separator=',' close=') '>",
            "#{item}",
            "</foreach>",
            " and catnum in ",
            "<foreach item='item1' index='index' collection='catNums' open='(' separator=',' close=') '>",
            "#{item1}",
            "</foreach>",
            "</script>"})
    List<BenzEPCPartsInfoDTO> findPartsByPartsNameIdxAndCatNum(@Param("nounidxList") List<String> partsNameIdxes, @Param("catNums") List<String> catNums, @Param("types") List<String> types);

    @Results(value = {
            @Result(column = "subgrp", property = "subGroup"),
            @Result(column = "nounidx", property = "partsNameIdx"),
            @Result(column = "codeB", property = "saCode"),
            @Result(column = "parttype", property = "partsType"),
            @Result(column = "replflg", property = "replFlag"),
            @Result(column = "SUBMODS", property = "engineEnd3Str")
    })
    @Select({"<script>select id,catnum,groupnum,subgrp,parttype,partnum,nounidx,descidx,codeb,footnotes,replflg,SUBMODS,steering from bm_parts2_v where  " +
            "  type in ",
            "<foreach item='type' index='index' collection='types' open='(' separator=',' close=') '>",
            "#{type}",
            "</foreach>",
            " and nounidx in ",
            "<foreach item='item' index='index' collection='nounidxList' open='(' separator=',' close=') '>",
            "#{item}",
            "</foreach>",
            " and catnum in ",
            "<foreach item='item1' index='index' collection='catNums' open='(' separator=',' close=') '>",
            "#{item1}",
            "</foreach>",
            " and descidx in ",
            "<foreach item='item2' index='index' collection='descIdxList' open='(' separator=',' close=') '>",
            "#{item2}",
            "</foreach>",
            "</script>"})
    List<BenzEPCPartsInfoDTO> findPartsByNameIdxDescIdxAndCatNum(@Param("types") List<String> types, @Param("nounidxList") List<String> partsNameIdxes, @Param("descIdxList") List<String> descIdxes, @Param("catNums") List<String> catNums);

    @Results(value = {
            @Result(column = "subgrp", property = "subGroup"),
            @Result(column = "nounidx", property = "partsNameIdx"),
            @Result(column = "codeB", property = "saCode"),
            @Result(column = "parttype", property = "partsType"),
            @Result(column = "POS_ADDR", property = "posAddr"),
            @Result(column = "replflg", property = "replFlag")
    })
    @Select({"select id,catnum,groupnum,subgrp,seqnum,seqno,callout,replflg,parttype,partnum,nounidx,",
            "descidx,indent,tuvsign,optflag,optPart,steering,type,reptype,reppno,footnotes,neutral,codeb,",
            "quantbm,components,submods,reppart,POS_ADDR,cbPartsCode from bm_parts2_v ",
            " where catnum = #{p.catNum} and groupnum = #{p.groupNum} ",
            "and subgrp = #{p.subGroup} and callout = #{p.callout} ",
            "and cbPartsCode = #{partsCode}  "})
    List<BenzEPCPartsInfoDTO> findPartsByCatNumsAndPartsCode(@Param("p") BenzEPCPartsInfoDTO p, @Param("partsCode") String partsCode);


    @Results(value = {
            @Result(column = "subgrp", property = "subGroup"),
            @Result(column = "nounidx", property = "partsNameIdx"),
            @Result(column = "codeB", property = "saCode"),
            @Result(column = "parttype", property = "partsType"),
            @Result(column = "POS_ADDR", property = "posAddr"),
            @Result(column = "replflg", property = "replFlag")
    })
    @Select({"<script>select id,catnum,groupnum,subgrp,seqnum,seqno,callout,replflg,parttype,partnum,nounidx,",
            "descidx,indent,tuvsign,optflag,optPart,steering,type,reptype,reppno,footnotes,neutral,codeb,",
            "quantbm,components,submods,reppart,POS_ADDR,cbPartsCode from bm_parts2_v ",
            "where (catnum, groupnum, subgrp, callout, cbPartsCode) in",
            "<foreach item='item' index='index' collection='partsInfo' open='(' separator=',' close=')'>",
            "(#{item.catNum}, #{item.groupNum}, #{item.subGroup}, #{item.callout}, #{item.cbPartsCode})",
            "</foreach>",
            "</script>"})
    List<BenzEPCPartsInfoDTO> findPartsByRepInfo(@Param("partsInfo") Collection<BenzEPCPartsInfoDTO> partsInfo);

    @Results(value = {
            @Result(column = "subgrp", property = "subGroup"),
            @Result(column = "nounidx", property = "partsNameIdx"),
            @Result(column = "codeB", property = "saCode"),
            @Result(column = "parttype", property = "partsType"),
            @Result(column = "POS_ADDR", property = "posAddr"),
            @Result(column = "replflg", property = "replFlag")
    })
    @Select({"<script>select id,catnum,groupnum,subgrp,seqnum,seqno,callout,replflg,parttype,partnum,nounidx," +
            "descidx,indent,tuvsign,optflag,optPart,steering,type,reptype,reppno,footnotes,neutral,codeb," +
            "quantbm,components,submods,reppart,POS_ADDR,cbPartsCode from bm_parts2_v   " +
            "  where id in" +
            "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=') '>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<BenzEPCPartsInfoDTO> findPartsByCatNumsAndPartsName(@Param("ids") List<Integer> ids);

    @Select("select id,partsName,partsCode from tmp_benz_parts where id > 68760")
    List<BenzEPCPartsInfoDTO> findOnlineBenzParts();

    @Results(value = {
            @Result(column = "subgrp", property = "subGroup"),
            @Result(column = "nounidx", property = "partsNameIdx"),
            @Result(column = "codeB", property = "saCode"),
            @Result(column = "parttype", property = "partsType"),
            @Result(column = "replflg", property = "replFlag"),
            @Result(column = "SUBMODS", property = "engineEnd3Str")
    })

    @Select({"<script>select DESCRIPTION from part_descs_v where lang='Z' and DESCIDX in",
            " <foreach item='descIdx' index='index' collection='descSet' open='(' separator=',' close=') '>",
            "#{descIdx}",
            "</foreach>",
            "</script>"})
    List<String> getDescsName(@Param("descSet") Set<String> descSet);

    @Select({"<script>select distinct partsCode, replaceCode from benz_replace_code",
            "where partsCode in ",
            " <foreach item='partsCode' index='index' collection='partsCodes' open='(' separator=',' close=') '>",
            "#{partsCode}",
            "</foreach>",
            "and catNum in ",
            " <foreach item='catNum' index='index' collection='catNums' open='(' separator=',' close=') '>",
            "#{catNum}",
            "</foreach>",
            "</script>"})
    List<BenzReplaceCodeDTO> queryReplaceCodes(@Param("partsCodes") Set<String> partsCodes, @Param("catNums") List<String> catNums);

    @Select({"<script>select id,catnum,groupnum,subgrp subGroup,seqnum,seqno,callout,replflg replFlag,parttype partsType,partnum,nounidx partsNameIdx,",
            "descidx,indent,tuvsign,optflag,optPart,steering,type,reptype,reppno,footnotes,neutral,codeb saCode,",
            "quantbm,components,submods,reppart,POS_ADDR posAddr,cbPartsCode from bm_parts2_v ",
            "where callout != ''",
            "and (catnum, groupnum, subgrp) in ",
            "<foreach item='item' collection='partsInfo' index='index' open='(' separator=',' close=')'>",
            "(#{item.catNum}, #{item.groupNum}, #{item.subGroup})",
            "</foreach>",
            "</script>"})
    List<BenzEPCPartsInfoDTO> findPartsByCatNumGroupNumSubGrp(@Param("partsInfo") Collection<BenzEPCPartsInfoDTO> partsInfo);


    @Results(value = {
            @Result(property = "catNum", column = "CATNUM"),
            @Result(property = "indent", column = "INDENT"),
            @Result(property = "descIdx", column = "DESCIDX"),
            @Result(property = "nounIdx", column = "NOUNIDX"),
            @Result(property = "seqNo", column = "SEQNO"),
            @Result(property = "footnotes", column = "FOOTNOTES"),
            @Result(property = "repType", column = "REPTYPE"),
            @Result(property = "optPart", column = "OPTPART"),
            @Result(property = "replFlag", column = "REPLFLG"),
            @Result(property = "callout", column = "CALLOUT"),
            @Result(property = "optFlag", column = "OPTFLAG"),
            @Result(property = "group", column = "GROUPNUM"),
            @Result(property = "steering", column = "STEERING"),
            @Result(property = "tuvSign", column = "TUVSIGN"),
            @Result(property = "componentType", column = "COMPONENTS"),
            @Result(property = "components", column = "COMPONENTS"),
            @Result(property = "type", column = "TYPE"),
            @Result(property = "subGroup", column = "SUBGRP"),
            @Result(property = "repPartNo", column = "REPPNO"),
            @Result(property = "partType", column = "PARTTYPE"),
            @Result(property = "partNum", column = "PARTNUM"),
            @Result(property = "qty", column = "QUANTBM"),
            @Result(property = "neutral", column = "NEUTRAL"),
            @Result(property = "partRem", column = "PARTREM"),
            @Result(property = "saCode", column = "CODEB"),
            @Result(property = "subMods", column = "SUBMODS"),
            @Result(property = "repPart", column = "REPPART"),
            @Result(property = "seqNum", column = "SEQNUM"),
            @Result(property = "positionAddress", column = "POS_ADDR"),
            @Result(property = "vpdIdent", column = "VPD_IDENT"),
            @Result(property = "btrPartsCode", column = "cbPartsCode"),
    })
    @Select({"<script>",
            "SELECT CATNUM,INDENT, DESCIDX, NOUNIDX, SEQNO, FOOTNOTES, REPTYPE, OPTPART, REPLFLG, ",
            "CALLOUT, OPTFLAG, GROUPNUM, STEERING, TUVSIGN, COMPONENTS, TYPE,",
            " GROUPNUM,SUBGRP, REPPNO, PARTTYPE, PARTNUM, QUANTBM, NEUTRAL,",
            " PARTREM, CODEB, SUBMODS, REPPART, SEQNUM, ",
            " POS_ADDR, VPD_IDENT,cbPartsCode from BM_PARTS2_V a ",
            "where ",
            " <if test='mainGroupNo != null'>",
            "  GROUPNUM = #{mainGroupNo} AND ",
            " </if>",
            " <if test='subGroupNo != null'>",
            "  SUBGRP = #{subGroupNo} AND ",
            " </if>",
            " <if test='partsCode != null'>",
            "  cbPartsCode = #{partsCode} AND ",
            " </if>",
            "CATNUM IN ",
            "<foreach item='catNum' collection='carNos' index='index' open='(' separator=',' close=')'>",
            "#{catNum}",
            "</foreach> ",
            " order by SEQNUM",
            "</script>"})
    List<BenzSuperPartsInfoDTO> queryPartsInfoByCar(@Param("carNos") Collection<String> carNos,
                                                    @Param("mainGroupNo") String mainGroupNo,
                                                    @Param("subGroupNo") String subGroupNo,
                                                    @Param("partsCode") String partsCode);


    /**
     * 查询配件解释
     *
     * @param descIndexs
     * @param landCode
     * @return
     */
    @Results(value = {
            @Result(column = "DESCIDX", property = "descIndex"),
            @Result(column = "description", property = "desc")
    })
    @Select({"<script>",
            "select descidx,description FROM part_descs_v",
            " where LANG = #{landCode} AND DESCIDX IN ",
            "<foreach item='descIndex' collection='descIndexs' index='index' open='(' separator=',' close=')'>",
            "#{descIndex}",
            "</foreach>",
            "</script>"})
    List<BenzSuperDescDTO> queryPartEpcDesc(@Param("descIndexs") Collection<String> descIndexs, @Param("landCode") String landCode);


    @Select({"<script>",
            "select descidx from bm_parts2_v where ",
            "  DESCIDX IN ",
            "<foreach item='descIndex' collection='descIndexs' index='index' open='(' separator=',' close=')'>",
            "#{descIndex}",
            "</foreach>",
            " and  catNum IN ",
            "<foreach item='catNum' collection='catNums' index='index' open='(' separator=',' close=')'>",
            "#{catNum}",
            "</foreach>",
            "</script>"})
    Set<String> findPartsDescIdx(@Param("descIndexs") Collection<String> descIndexs, @Param("catNums") Collection<String> catNums);


    /**
     * 查询epc 名称
     *
     * @param nouns
     * @return
     */
    @Results(value = {
            @Result(column = "NOUN", property = "noun"),
            @Result(column = "NOUNIDX", property = "nounIdx")
    })
    @Select({"<script>",
            "select NOUNIDX,NOUN FROM part_nouns_v",
            " where NOUN IN ",
            "<foreach item='noun' collection='nouns' index='index' open='(' separator=',' close=')'>",
            "#{noun}",
            "</foreach>",
            "</script>"})
    List<BenzEPCPartsNameDTO> queryPartEpcNoun(@Param("nouns") Collection<String> nouns);


    @Select({"<script>",
            "select NOUNIDX from bm_parts2_v where ",
            "  NOUNIDX IN ",
            "<foreach item='benzNounIdx' collection='benzNounIdxs' index='index' open='(' separator=',' close=')'>",
            "#{benzNounIdx}",
            "</foreach>",
            " and  catNum IN ",
            "<foreach item='catNum' collection='catNums' index='index' open='(' separator=',' close=')'>",
            "#{catNum}",
            "</foreach>",
            "</script>"})
    Set<String> findPartsByNounIdx(@Param("benzNounIdxs") Collection<String> benzNounIdxs, @Param("catNums") Collection<String> catNums);
}
